﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
using System.Reflection;
using Microsoft.Win32;
using System.Security;
using System.Diagnostics;

namespace AeonModStreamedMPEditor
{
    public partial class AeonModStreamedMPEditor
    {


        private void readFonts()
        {
            XmlDocument doc = new XmlDocument();
            if (!File.Exists(mpPaths.streamedMPpath + "Fonts.xml"))
            {
                showError("Fonts.xml Not Found in \r\r" + mpPaths.streamedMPpath + " \r\rPlease make sure fonts.xml exists", errorCode.major);
                return;
            }
            try
            {
                doc.Load(mpPaths.streamedMPpath + "Fonts.xml");
            }
            catch (Exception e)
            {
                showError("Exception while loading Fonts.xml\n\n" + e.Message, errorCode.loadError);
                return;
            }

            XmlNodeList fonts = doc.DocumentElement.SelectNodes("/fonts/font");
            skinFonts1.Clear();
            foreach (XmlNode font in fonts)
            {
                skinFonts sknFont = new skinFonts();
                XmlNode innerNode = font.SelectSingleNode("name");
                skinFonts1.Add(innerNode.InnerText);

                //fill out the font class
                sknFont.fontName = innerNode.InnerText;
                innerNode = font.SelectSingleNode("filename");
                sknFont.fontFileName = innerNode.InnerText;
                innerNode = font.SelectSingleNode("height");
                sknFont.fontHeight = int.Parse(innerNode.InnerText);
                innerNode = font.SelectSingleNode("start");
                sknFont.fontStart = int.Parse(innerNode.InnerText);
                innerNode = font.SelectSingleNode("end");
                sknFont.fontEnd = int.Parse(innerNode.InnerText);
                innerNode = font.SelectSingleNode("bold");
                if (innerNode != null)
                {
                    if (innerNode.InnerText.ToLower() == "yes")
                        sknFont.fontBold = true;
                }
                innerNode = font.SelectSingleNode("italic");
                if (innerNode != null)
                {
                    if (innerNode.InnerText.ToLower() == "yes")
                        sknFont.fontItalic = true;
                }

                //skFonts.Add(new skinFontsBinding(sknFont.fontName, sknFont.fontFileName,sknFont.fontHeight,sknFont.fontStart,sknFont.fontEnd, sknFont.fontBold,sknFont.fontItalic));
                skFonts.Add(sknFont);
            }


            List<string> skinFonts2 = new List<string>(skinFonts1);
            List<string> skinFonts3 = new List<string>(skinFonts1);

            cboSelectedFont.DataSource = skinFonts1;
            cboLabelFont.DataSource = skinFonts2;
            fontsList.DataSource = skinFonts3;
            cboFontFileName.DataSource = systemFonts;
            displayFontDetails();
            setFontFields(WeAre.notEditing);

        }


        private void setFontFields(WeAre Editing)
        {
            if (Editing == WeAre.editing)
            {
                // Always disabled
                tbFontName.Enabled = false;

                cboFontFileName.Enabled = true;
                tbFontHeight.Enabled = true;
                tbFontStartChar.Enabled = true;
                tbFontStartChar.Enabled = true;
                tbFontEndChar.Enabled = true;
                cbFontBold.Enabled = true;
                cbFontItalic.Enabled = true;

                btFontCancel.Enabled = true;
                btFontSave.Enabled = true;
                btFontEdit.Enabled = false;
            }
            else
            {
                cboFontFileName.Enabled = false;
                tbFontHeight.Enabled = false;
                tbFontStartChar.Enabled = false;
                tbFontStartChar.Enabled = false;
                tbFontEndChar.Enabled = false;
                cbFontBold.Enabled = false;
                cbFontItalic.Enabled = false;

                btFontCancel.Enabled = false;
                btFontSave.Enabled = false;
                btFontEdit.Enabled = true;

            }
        }



        private void btFontCancel_Click(object sender, EventArgs e)
        {
            setFontFields(WeAre.notEditing);
        }


        private void fontsList_SelectedIndexChanged(object sender, EventArgs e)
        {
            displayFontDetails();
            setFontFields(WeAre.notEditing);

        }

        private void displayFontDetails()
        {
            foreach (skinFonts sf in skFonts)
            {
                if (sf.fontName == fontsList.Text)
                {
                    tbFontName.Text = sf.fontName;
                    cboFontFileName.Text = sf.fontFileName;
                    tbFontHeight.Text = sf.fontHeight.ToString();
                    tbFontStartChar.Text = sf.fontStart.ToString();
                    tbFontEndChar.Text = sf.fontEnd.ToString();
                    cbFontBold.Checked = sf.fontBold;
                    cbFontItalic.Checked = sf.fontItalic;
                }
            }
        }


        private void btFontEdit_Click(object sender, EventArgs e)
        {
            setFontFields(WeAre.editing);
        }

        private void btFontSave_Click(object sender, EventArgs e)
        {
            foreach (skinFonts sf in skFonts)
            {
                if (sf.fontName == fontsList.Text)
                {
                    sf.fontFileName = cboFontFileName.Text;
                    sf.fontHeight = int.Parse(tbFontHeight.Text);
                    sf.fontStart = int.Parse(tbFontStartChar.Text);
                    sf.fontEnd = int.Parse(tbFontEndChar.Text);
                    sf.fontBold = cbFontBold.Checked;
                    sf.fontItalic = cbFontItalic.Checked;
                }
            }
            setFontFields(WeAre.notEditing);
        }


        private void btFontWrite_Click(object sender, EventArgs e)
        {

            string skeletonFile = "StreamedMP_AeonMod_Editor.xmlFiles.fontsSkeleton.xml";

            Stream stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(skeletonFile);
            StreamReader reader = new StreamReader(stream);
            xml = reader.ReadToEnd();

            StringBuilder rawXML = new StringBuilder();

            foreach (skinFonts sf in skFonts)
            {
                rawXML.AppendLine("<font>");
                rawXML.AppendLine("\t<name>" + sf.fontName + "</name>");
                rawXML.AppendLine("\t<filename>" + sf.fontFileName + "</filename>");
                rawXML.AppendLine("\t<height>" + sf.fontHeight.ToString() + "</height>");
                rawXML.AppendLine("\t<start>" + sf.fontStart.ToString() + "</start>");
                rawXML.AppendLine("\t<end>" + sf.fontEnd + "</end>");
                if (sf.fontBold)
                    rawXML.AppendLine("\t<bold>yes</bold>");
                else
                    rawXML.AppendLine("\t<bold>no</bold>");

                if (sf.fontItalic)
                    rawXML.AppendLine("\t<italic>yes</italic>");
                else
                    rawXML.AppendLine("\t<italic>no</italic>");
                rawXML.AppendLine("</font>\n");
            }

            xml = xml.Replace("<!-- Font Definitions -->", rawXML.ToString());

            if (System.IO.File.Exists(mpPaths.streamedMPpath + "fonts.xml"))
                System.IO.File.Copy(mpPaths.streamedMPpath + "fonts.xml", mpPaths.streamedMPpath + "fonts.xml.backup." + DateTime.Now.Ticks.ToString());

            if (System.IO.File.Exists(mpPaths.streamedMPpath + "fonts.xml"))
                System.IO.File.Delete(mpPaths.streamedMPpath + "fonts.xml");

            StreamWriter writer;
            writer = System.IO.File.CreateText(mpPaths.streamedMPpath + "fonts.xml");
            writer.Write(xml);
            writer.Close();
            string f1 = cboSelectedFont.Text;
            string f2 = cboLabelFont.Text;
            skFonts.Clear();
            readFonts();
            cboSelectedFont.Text = f1;
            cboLabelFont.Text = f2;

        }

        private void btFontRestore_Click(object sender, EventArgs e)
        {
            System.IO.StreamWriter writer;


            if (System.IO.File.Exists(mpPaths.streamedMPpath + "fonts.xml"))
                System.IO.File.Delete(mpPaths.streamedMPpath + "fonts.xml");

            Stream streamTemplate = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("StreamedMP_AeonMod_Editor.xmlFiles.defaultFonts.xml");
            StreamReader reader = new StreamReader(streamTemplate);
            xmlTemplate = reader.ReadToEnd();

            writer = System.IO.File.CreateText(mpPaths.streamedMPpath + "fonts.xml");
            writer.Write(xmlTemplate);
            writer.Close();
            showError("fonts.xml has been restored to default skin version", errorCode.info);
            string f1 = cboSelectedFont.Text;
            string f2 = cboLabelFont.Text;
            skFonts.Clear();
            readFonts();
            cboSelectedFont.Text = f1;
            cboLabelFont.Text = f2;
        }




        #region Classes

        public class skinFonts
        {
            public string fontName;
            public string fontFileName;
            public int fontHeight;
            public int fontStart;
            public int fontEnd;
            public bool fontBold;
            public bool fontItalic;
        }

        public class skinFontsBinding
        {
            private string fontName;
            private string fontFileName;
            private int fontHeight;
            private int fontStart;
            private int fontEnd;
            private bool fontBold;
            private bool fontItalic;
            public skinFontsBinding(string s1, string s2, int i1, int i2, int i3, bool b1, bool b2)
            {
                fontName = s1;
                fontFileName = s2;
                fontHeight = i1;
                fontStart = i2;
                fontEnd = i3;
                fontBold = b1;
                fontItalic = b2;
            }
            public string FontName
            {
                get { return fontName; }
                set { fontName = value; }
            }
            public string FontFileName
            {
                get { return fontFileName; }
                set { fontFileName = value; }
            }
            public int FontHeight
            {
                get { return fontHeight; }
                set { fontHeight = value; }
            }
            public int FontStart
            {
                get { return fontStart; }
                set { fontStart = value; }
            }
            public int FontEnd
            {
                get { return fontEnd; }
                set { fontEnd = value; }
            }
            public bool FontBold
            {
                get { return fontBold; }
                set { fontBold = value; }
            }
            public bool FontItalic
            {
                get { return fontItalic; }
                set { fontItalic = value; }
            }

        }
        #endregion
    }
}